package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.VirtualCharacterDTO;
import com.xuedongzhilian.entity.VirtualCharacter;
import com.xuedongzhilian.service.IVirtualCharacterService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 虚拟形象控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/virtual-character")
public class VirtualCharacterController {

    @Resource
    private IVirtualCharacterService virtualCharacterService;

    /**
     * 创建虚拟形象
     */
    @PostMapping
    public Result createVirtualCharacter(@RequestBody VirtualCharacterDTO virtualCharacterDTO) {
        Long userId = UserHolder.getUser().getId();
        
        // 检查用户是否已有虚拟形象
        VirtualCharacter existingCharacter = virtualCharacterService.lambdaQuery()
                .eq(VirtualCharacter::getUserId, userId)
                .one();
                
        if (existingCharacter != null) {
            return Result.fail("您已拥有虚拟形象");
        }
        
        VirtualCharacter virtualCharacter = new VirtualCharacter();
        BeanUtils.copyProperties(virtualCharacterDTO, virtualCharacter);
        virtualCharacter.setUserId(userId);
        virtualCharacter.setCreateTime(LocalDateTime.now());
        virtualCharacter.setUpdateTime(LocalDateTime.now());
        
        boolean success = virtualCharacterService.save(virtualCharacter);
        if (success) {
            return Result.ok(virtualCharacter);
        } else {
            return Result.fail("创建虚拟形象失败");
        }
    }

    /**
     * 获取用户虚拟形象
     */
    @GetMapping
    public Result getUserVirtualCharacter() {
        Long userId = UserHolder.getUser().getId();
        VirtualCharacter virtualCharacter = virtualCharacterService.lambdaQuery()
                .eq(VirtualCharacter::getUserId, userId)
                .one();
        return Result.ok(virtualCharacter);
    }

    /**
     * 更新虚拟形象
     */
    @PutMapping
    public Result updateVirtualCharacter(@RequestBody VirtualCharacterDTO virtualCharacterDTO) {
        Long userId = UserHolder.getUser().getId();
        VirtualCharacter existingCharacter = virtualCharacterService.lambdaQuery()
                .eq(VirtualCharacter::getUserId, userId)
                .one();
                
        if (existingCharacter == null) {
            return Result.fail("虚拟形象不存在");
        }
        
        VirtualCharacter virtualCharacter = new VirtualCharacter();
        BeanUtils.copyProperties(virtualCharacterDTO, virtualCharacter);
        virtualCharacter.setId(existingCharacter.getId());
        virtualCharacter.setUserId(userId);
        virtualCharacter.setUpdateTime(LocalDateTime.now());
        
        boolean success = virtualCharacterService.updateById(virtualCharacter);
        if (success) {
            return Result.ok(virtualCharacter);
        } else {
            return Result.fail("更新虚拟形象失败");
        }
    }

    /**
     * 喂食虚拟形象
     */
    @PostMapping("/feed")
    public Result feedVirtualCharacter() {
        Long userId = UserHolder.getUser().getId();
        VirtualCharacter virtualCharacter = virtualCharacterService.lambdaQuery()
                .eq(VirtualCharacter::getUserId, userId)
                .one();
                
        if (virtualCharacter == null) {
            return Result.fail("虚拟形象不存在");
        }
        
        // 更新心情值和能量值
        virtualCharacter.setMood(Math.min(100, virtualCharacter.getMood() + 10));
        virtualCharacter.setEnergy(Math.min(100, virtualCharacter.getEnergy() + 20));
        virtualCharacter.setUpdateTime(LocalDateTime.now());
        
        boolean success = virtualCharacterService.updateById(virtualCharacter);
        if (success) {
            return Result.ok(virtualCharacter);
        } else {
            return Result.fail("喂食失败");
        }
    }

    /**
     * 获取所有虚拟形象类型
     */
    @GetMapping("/types")
    public Result getVirtualCharacterTypes() {
        // 预定义的虚拟形象类型
        List<String> types = Arrays.asList("猫", "狗", "仓鼠", "兔子", "狐狸");
        return Result.ok(types);
    }
}